package br.com.musclebook.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import br.com.musclebook.enums.Sex;
import br.com.musclebook.enums.Situation;

@Entity
@NamedQueries({ @NamedQuery(name = "User.findById", query = "select u from User u where u.id =:id"), @NamedQuery(name = "User.authenticate", query = "select u from User u where u.password = :password and u.username = :username") })
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;
    private String lastName;
    private String nickName;

    @Enumerated(EnumType.ORDINAL)
    private Sex sex;

    @Temporal(TemporalType.DATE)
    private Date birthdate;

    @Size(min = 5, max = 20)
    private String username;

    @Size(min = 6, max = 12)
    private String password;

    private String email;

    @Lob
    private byte[] image;

    @Enumerated(EnumType.ORDINAL)
    private Situation situation;

    @OneToOne(mappedBy = "user", cascade = { CascadeType.ALL }, orphanRemoval = true)
    private UserCurrentRoutine currentRoutine;

    @OneToMany(mappedBy = "user", cascade = { CascadeType.ALL }, orphanRemoval = true)
    private List<UserExercise> userExercises;

    @OneToMany(mappedBy = "user", cascade = { CascadeType.ALL }, orphanRemoval = true)
    private List<Routine> routines;

    @Transient
    private List<Contact> contacts;

    public User() {
	this.situation = Situation.ACTIVE;
	this.routines = new ArrayList<Routine>();
	this.contacts = new ArrayList<Contact>();
    }

    public User(Long id) {
	this();
	this.id = id;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public String getFirstName() {
	return firstName;
    }

    public void setFirstName(String firstName) {
	this.firstName = firstName;
    }

    public String getLastName() {
	return lastName;
    }

    public void setLastName(String lastName) {
	this.lastName = lastName;
    }

    public String getNickName() {
	return nickName;
    }

    public void setNickName(String nickName) {
	this.nickName = nickName;
    }

    public Sex getSex() {
	return sex;
    }

    public void setSex(Sex sex) {
	this.sex = sex;
    }

    public Date getBirthdate() {
	return birthdate;
    }

    public void setBirthdate(Date birthdate) {
	this.birthdate = birthdate;
    }

    public String getUsername() {
	return username;
    }

    public void setUsername(String username) {
	this.username = username;
    }

    public String getPassword() {
	return password;
    }

    public void setPassword(String password) {
	this.password = password;
    }

    public String getEmail() {
	return email;
    }

    public void setEmail(String email) {
	this.email = email;
    }

    public byte[] getImage() {
	return image;
    }

    public void setImage(byte[] image) {
	this.image = image;
    }

    public Situation getSituation() {
	return situation;
    }

    public void setSituation(Situation situation) {
	this.situation = situation;
    }

    public UserCurrentRoutine getCurrentRoutine() {
	return currentRoutine;
    }

    public void setCurrentRoutine(UserCurrentRoutine currentRoutine) {
	this.currentRoutine = currentRoutine;
    }

    public List<UserExercise> getUserExercises() {
	return userExercises;
    }

    public void setUserExercises(List<UserExercise> userExercises) {
	this.userExercises = userExercises;
    }

    public List<Routine> getRoutines() {
	return routines;
    }

    public void setRoutines(List<Routine> routines) {
	this.routines = routines;
    }

    public List<Contact> getContacts() {
	return contacts;
    }

    public void setContacts(List<Contact> contacts) {
	this.contacts = contacts;
    }

}
